//
//  OMShoppingCartGoodTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/22.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMShoppingCartGoodTableCell.h"
#import "OMCartProduct.h"
@implementation OMShoppingCartGoodTableCell {
    __weak OMCartProduct *_good;
}

- (void)configure
{
    [super configure];
    [self.contentView addSubview:self.checkBoxBtn];
    self.titleLabel.numberOfLines = 2;

    [self.checkBoxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(0)));
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.width.height.equalTo(@(SM_SIZE(90)));
    }];

    [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SM_SIZE(25)));
        make.left.equalTo(self.checkBoxBtn.mas_right).offset(SM_SIZE(0));
        make.bottom.equalTo(@(SM_SIZE(-25)));
        make.width.equalTo(self.iconImageView.mas_height);
    }];

    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(SM_SIZE(20));
        make.top.equalTo(self.iconImageView.mas_top).offset(SM_SIZE(10));

        make.right.equalTo(@(SM_SIZE(-20)));
    }];

    self.priceLabel.textColor = SM_blackColor;
    self.priceLabel.font = SM_NFont(28);
    self.titleLabel.font = SM_CFont(30);
    self.titleLabel.textColor = SM_blackColor;

    _specLabel = [UILabel new];
    _specLabel.text = @" ";
    _specLabel.textColor = HEXCOLOR(@"#646464");
    _specLabel.font = SM_CFont(25);
    [self.contentView addSubview:_specLabel];
    [_specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(SM_SIZE(3));
    }];

    [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.bottom.equalTo(self.iconImageView.mas_bottom);
    }];

}

- (void)configureCellWithObj:(OMCartProduct *)good
{
    _good = good;
    [self.iconImageView kt_imageWithURLStr:good.image placeHolder:[UIImage imageNamed:@"placehold_item"] withSize:CGSizeZero];
    self.num = good.quantity;
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString: good.brand?:@"" attributes:@{NSFontAttributeName:SM_CBFont(30)}];
    !title.string.length?:[title appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [title appendAttributedString:[[NSAttributedString alloc] initWithString:good.name?:@"" attributes:@{NSFontAttributeName:SM_CFont(30)}]];
    self.titleLabel.attributedText = title;
    _specLabel.text = good.unit?:@"3 ";
    self.priceLabel.text = good.price.floatValue == 0 ?@"线上预约":[[NSString stringWithFormat:@"￥%.2f",good.price.floatValue]  stringByReplacingOccurrencesOfString:@".00" withString:@""];
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//
//}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [super touchesEnded:touches withEvent:event];
//    UITouch *touch = [touches anyObject];
//    CGPoint location = [touch locationInView:self.contentView];
//    if (location.x < SM_SIZE(86)) {
//        [self checkBoxBtnClick];
//    }else {
//
//        if (!CGRectContainsPoint(self.plusBtn.frame, location) && !CGRectContainsPoint(self.reduceBtn.frame, location)) {
//            NaviPush(@"OMGoodDetailViewController", @{@"productId":_good.productId});
//        }
//    }
//}


- (UIButton *)checkBoxBtn
{
    if (!_checkBoxBtn) {
        _checkBoxBtn = [UIButton new];
        [_checkBoxBtn setImage:[UIImage imageNamed:@"btn_shoppingcart"] forState:UIControlStateNormal];
        [_checkBoxBtn setImage:[UIImage imageNamed:@"btn_shoppingcart_hl"] forState:UIControlStateSelected];
        _checkBoxBtn.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(25), SM_SIZE(25), SM_SIZE(25), SM_SIZE(25));
        [_checkBoxBtn addTarget:self action:@selector(checkBoxBtnClick) forControlEvents:UIControlEventTouchUpInside];
//        _checkBoxBtn.userInteractionEnabled = NO;
    }
    return _checkBoxBtn;
}
- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton new];
        [_cancelBtn setImage:[UIImage imageNamed:@"ic_cart_item_cancel"] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (void)checkBoxBtnClick
{
    self.checkBoxBtn.selected = !self.checkBoxBtn.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickCheckBox:atIndexPath:)]) {
        [self.delegate didClickCheckBox:self.checkBoxBtn atIndexPath:[(UITableView *)self.superview.superview indexPathForCell:self]];
    }
//    [UIView animateWithDuration:.15 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
//        self.checkBoxBtn.transform = CGAffineTransformMakeScale(1.1, 1.1);
//    } completion:^(BOOL finished) {
//
//        self.checkBoxBtn.transform = CGAffineTransformIdentity;
//    }];

}

- (void)cancelBtnClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickCancelButton:atIndexPath:)]) {
        [self.delegate didClickCancelButton:self.cancelBtn atIndexPath:[(UITableView *)self.superview.superview indexPathForCell:self]];
    }
}

- (void)setOm_selected:(BOOL)om_selected
{
    _om_selected = om_selected;
    self.checkBoxBtn.selected = self.om_selected;
}
@end
